package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/*
    库管类
        使用集合来作为数据仓库
 */
public class OtherStudentDao implements BaseStudentDao {

    //1. 定义一个长度为5的数组,作为整个系统的数据容器
    private static ArrayList<Student> stus = new ArrayList<Student>();

    //静态代码块: 在系统进行类加载时读取文件数据进行信息的初始化
    static {
        System.out.println("---------------loading--------------------");
        //load01();
        try {
            load05();
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    //库管类中的添加功能
    public boolean addStudent(Student stu) {
        stus.add(stu);
        //reSave01();
        reSave05();
        return true;

    }

    //库管类中的查询功能
    public Student[] findAllStudents() {
        return stus.toArray(new Student[stus.size()]);
    }

    //库管类中的删除功能
    public void deleteStudentId(String delId) {
        int index = getIndex(delId);
        stus.remove(index);
//        reSave01();
        reSave05();
    }

    //库管类中的修改功能
    public void updateStudentById(String updateId, Student newStu) {
        int index = getIndex(updateId);
        stus.set(index, newStu);
//        reSave01();
        reSave05();
    }

    //根据学号查找对应学生对象在数组容器中的索引位置
    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stus.size(); i++) {
            if (stus.get(i) != null && stus.get(i).getSid().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }


    //将系统中最新的数据写出到文件,进行存档   --  基于普通字节输出流
    public void reSave01() {
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream("黑马学生管理系统2\\stu.txt"));
            for (Student student : stus) {
                bos.write(student.toString().getBytes());
                bos.write(System.lineSeparator().getBytes());
            }
        } catch (Exception e) {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ex) {
                    //ex.printStackTrace();
                }
            }
            //e.printStackTrace();
        }

    }

    //将上次存档的数据重新读回内存   --  基于普通字节输入流
    public static void load01() {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("黑马学生管理系统2\\stu.txt"));
            byte[] bys = new byte[1024 * 1024];
            int len = bis.read(bys);
            String students = new String(bys, 0, len);
            String[] student = students.split(System.lineSeparator());
            for (String stu : student) {
                String[] strings = stu.split(",");
                stus.add(new Student(strings[0], strings[1], strings[2], strings[3]));
            }
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }

    public void reSave05() {
        try {
            //
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream
                    ("黑马学生管理系统2\\stu.txt"));
            //
            oos.writeObject(stus);
            oos.close();
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }

    public static void load05() {
        try {
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream
                    ("黑马学生管理系统2\\stu.txt"));
            stus = (ArrayList<Student> )ois.readObject();
            ois.close();
        } catch (IOException e) {
           // e.printStackTrace();
        } catch (ClassNotFoundException e) {
           // e.printStackTrace();
        }
    }

}
